﻿namespace Hidistro.SqlDal.Store
{
    using Hidistro.Entities;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Data;
    using System.Data.Common;

    public class AppVersionRecordDao
    {
        private Database database = DatabaseFactory.CreateDatabase();

        public bool AddAppInstallRecord(AppInstallRecordInfo appInstallRecord)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("DELETE FROM Hishop_AppInstallRecords WHERE VID=@VID; INSERT INTO Hishop_AppInstallRecords(VID,Device) VALUES(@VID,@Device)");
            this.database.AddInParameter(sqlStringCommand, "VID", DbType.String, appInstallRecord.VID);
            this.database.AddInParameter(sqlStringCommand, "Device", DbType.String, appInstallRecord.Device);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool AddAppVersionRecord(AppVersionRecordInfo appVersionRecord)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("INSERT INTO Hishop_AppVersionRecords(Device,Version, Description, IsForcibleUpgrade, UpgradeUrl) VALUES(@Device,@Version, @Description, @IsForcibleUpgrade, @UpgradeUrl)");
            this.database.AddInParameter(sqlStringCommand, "Device", DbType.String, appVersionRecord.Device);
            this.database.AddInParameter(sqlStringCommand, "Version", DbType.Decimal, appVersionRecord.Version);
            this.database.AddInParameter(sqlStringCommand, "Description", DbType.String, appVersionRecord.Description);
            this.database.AddInParameter(sqlStringCommand, "IsForcibleUpgrade", DbType.Boolean, appVersionRecord.IsForcibleUpgrade);
            this.database.AddInParameter(sqlStringCommand, "UpgradeUrl", DbType.String, appVersionRecord.UpgradeUrl);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public AppVersionRecordInfo GetLatestAppVersionRecord(string device)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT TOP 1 * FROM Hishop_AppVersionRecords WHERE Device = @Device ORDER BY Version DESC");
            this.database.AddInParameter(sqlStringCommand, "Device", DbType.String, device);
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                return ReaderConvert.ReaderToModel<AppVersionRecordInfo>(reader);
            }
        }

        public bool IsForcibleUpgrade(string device, decimal version)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT IsForcibleUpgrade FROM Hishop_AppVersionRecords WHERE Version > @Version AND Device = @Device");
            this.database.AddInParameter(sqlStringCommand, "Device", DbType.String, device);
            this.database.AddInParameter(sqlStringCommand, "Version", DbType.Decimal, version);
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                while (reader.Read())
                {
                    if ((bool) reader[0])
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

